
package Type_1_arr.title4_P209;


//209. 长度最小的子数组
public class P209MiniMumSize2 {


    // TODO {2, 3, 1, 2, 4, 3}   滑动窗口
    public static int minSubArrayLen(int[] nums, int target) {

        int resultMin = Integer.MAX_VALUE;  //TODO 默认取最大值

        int winIndex = 0;     // 滑动窗口的左边界

        int tempMinSum = 0;   // 当前窗口内元素的和
        int tempMin;          // 当前窗口的长度
        for (int startIndex = 0; startIndex < nums.length; startIndex++) {

            tempMinSum += nums[startIndex];
            while (tempMinSum >= target) {
                //TODO 1、更新最小长度
                tempMin = startIndex - winIndex + 1;  //更新当前最小值
                resultMin = Math.min(tempMin, resultMin);  //取小的那个

                //TODO 2、滑动窗口向前移
                tempMinSum -= nums[winIndex];  //删除值
                winIndex++;      //前进
            }

        }

        return resultMin == Integer.MAX_VALUE ? 0 : resultMin;

    }

    public static void main(String[] args) {

        int[] nums = {2, 3, 1, 2, 4, 3};
        int target = 7;

        int min = minSubArrayLen(nums, target);
        System.out.println("min:" + min);
    }
}